package thread;

/**
 * demo05
 * 龟兔赛跑
 *
 * @author dingwen
 * @date 2021/09/05
 */
public class Demo05 implements Runnable{
    /**
     * 赢家
     */
    private static String winner;
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if(judge(i)){
                break;
            }
           String  currentThreadName = Thread.currentThread().getName();
            System.out.println(currentThreadName+"跑了"+i+"步");

            // 模拟兔子休息
            if (currentThreadName.equals("兔子") && i % 10 == 0){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
        
    }

    /**
     * 判断游戏是否结束
     *
     * @param steps 步骤
     * @return boolean
     */
    private boolean judge(int steps){
        if (winner != null){
            return true;
        }
        if (steps == 100){
            winner = Thread.currentThread().getName();
            System.out.println("赢家是："+winner);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Demo05 demo05 = new Demo05();
        new Thread(demo05,"乌龟").start();
        new Thread(demo05,"兔子").start();
    }
}
